package com.alinesno.cloud.portal.desktop.web.ucenter;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alinesno.cloud.base.boot.entity.ManagerAccountEntity;
import com.alinesno.cloud.base.boot.entity.ManagerRoleEntity;
import com.alinesno.cloud.base.boot.service.IManagerAccountService;
import com.alinesno.cloud.base.boot.service.IManagerRoleService;
import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.facade.pageable.DatatablesPageBean;
import com.alinesno.cloud.common.facade.response.ResponseBean;
import com.alinesno.cloud.common.facade.response.ResponseGenerator;
import com.alinesno.cloud.common.web.base.advice.TranslateCode;
import com.alinesno.cloud.common.web.base.controller.FeignMethodController;
import com.alinesno.cloud.common.web.login.aop.DataFilter;

/**
 * 后台账户管理 
 * @author WeiXiaoJin
 * @since 2019年3月24日 下午1:16:44
 */
@Controller
@Scope(SpringInstanceScope.PROTOTYPE)
@RequestMapping("portal/ucenter/account")
public class AccountController extends FeignMethodController<ManagerAccountEntity , IManagerAccountService> {

	private static final Logger log = LoggerFactory.getLogger(AccountController.class) ; 

	@Autowired
	private IManagerAccountService iManagerAccountService ; 
	
	@Autowired
	private IManagerRoleService iManagerRoleService ; 

	/**
	 * 树查询
	 */
	@GetMapping("/select")
    public void select(Model model , String applicationId){
		model.addAttribute("applicationId", applicationId) ; 
		log.debug("进入树列表选择页面.");
    }
	
	@GetMapping("/add_dept")
	public void addDept(Model model, HttpServletRequest request) {
		
	}

	@DataFilter
	@TranslateCode(value="[{hasStatus:has_status,rolePower:role_power}]" , plugin="departmentTranslatePlugin")
	@ResponseBody
	@PostMapping("/datatables")
    public DatatablesPageBean datatables(HttpServletRequest request , Model model ,  DatatablesPageBean page){
		return this.toPage(model, iManagerAccountService , page) ;
    }

	/**
	 * 角色用户
	 * @param request
	 * @param model
	 * @param page
	 * @return
	 */
	@DataFilter
	@TranslateCode(value="[{hasStatus:has_status,rolePower:role_power}]" , plugin="departmentTranslatePlugin")
	@ResponseBody
	@PostMapping("/roleMemberDatatables")
    public DatatablesPageBean memberDatatables(HttpServletRequest request , Model model , String roleId ,  DatatablesPageBean page){
//		RestWrapper wrapper = buildWrapper(page , null) ; 
		Page<ManagerAccountEntity> records = iManagerAccountService.findRoleMemberPage(roleId , page.buildWrapper())  ; 
		return this.toPage(model, iManagerAccountService , page , null ,records) ;
    }
	
	/**
	 * 认证
	 */
	@GetMapping("/auth")
    public ResponseBean auth(String accountId){
		log.debug("id = {}" , accountId);
		ManagerAccountEntity bean = iManagerAccountService.findEntityById(accountId) ; 
		return ResponseGenerator.ok(bean) ; 
    }

	/**
	 * 认证
	 */
	@ResponseBody
	@PostMapping("/role")
    public List<ManagerRoleEntity> role(String accountId){
		log.debug("id = {}" , accountId);
		List<ManagerRoleEntity> bean = iManagerRoleService.findByAccountId(accountId) ; 
		return bean ; 
    }
	
	/**
	 * 认证
	 */
	@ResponseBody
	@PostMapping("/authAccount")
    public ResponseBean authAccount(String accountId , String rolesId){
		log.debug("id = {}" , accountId);
	
		ManagerAccountEntity bean = iManagerAccountService.findEntityById(accountId) ; 
		boolean isAuth = iManagerRoleService.authAccount(bean, rolesId) ; 
		
		return ResponseGenerator.ok(isAuth) ; 
    }

	@Override
	public IManagerAccountService getFeign() {
		return iManagerAccountService;
	}

}
